package com.education.service.impl;

import com.education.dao.RegisterDao;
import com.education.model.*;
import com.education.service.RegisterService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2017/8/8.
 * <p>
 * 报名模块服务层
 */
@Service
public class RegisterServiceImpl implements RegisterService {


    @Autowired
    private RegisterDao registerDao;

    /**
     * 日志记录类
     */
    private static Logger MYLOGGER = LogManager.getLogger(StuIndexServiceImpl.class);

    /**
     * 传入报名信息
     *
     * @param register
     * @return
     * @throws Exception
     */
    @Override
    public int addRegisterInfomation(Register register) throws Exception {
        register.setEnterDate(new Date());
        int result = registerDao.addRegisterInfomation(register);

        return result;
    }

    /**
     * 2017/8/18 王娇
     * 查询所有专业
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<MajorModel> getMajor() throws Exception {
        List<MajorModel> list = registerDao.getMajor();
        return list;
    }


    /**
     * 2017/8/19 王娇
     * 生成入学测试题
     * 选择题
     *
     * @param scoreType
     * @return
     * @throws Exception
     */
    public Map<String, List> enterTestSelect(Integer scoreType) throws Exception {
        //题库中选择题   试题集合
        List<QuestionModel> selectlist = registerDao.enterTestSelect(scoreType);
        List<QuestionModel> judgeList = registerDao.enterTestJudge(scoreType);

        Set<QuestionModel> judgeSet = new HashSet<QuestionModel>();
        Set<QuestionModel> selectSet = new HashSet<QuestionModel>();
        Random ran = new Random();
        //随机出判断题
        while (true) {
            int num = ran.nextInt(judgeList.size());

            judgeSet.add(judgeList.get(num));

            if (judgeSet.size() == 5) {
                break;
            }
        }
        //随机出选择题
        while (true) {
            int num = ran.nextInt(selectlist.size());
            selectSet.add(selectlist.get(num));
            if (selectSet.size() == 5) {
                break;
            }
        }
        List selectList2 = new ArrayList(selectSet);
        List judgeList2 = new ArrayList(judgeSet);

        Map<String, List> map = new HashMap<String, List>();
        map.put("judgeTopics", judgeList2);
        map.put("selectTopics", selectList2);
        return map;
    }

    /**
     * 2017/8/21 王娇
     * 插入入学成绩
     *
     * @param total
     * @param enterPhone
     * @return
     * @throws Exception
     */
    @Override
    public Integer addEnterScore(Integer total, String enterPhone) throws Exception {
        int result = registerDao.addEnterScore(total, enterPhone);
        return result;
    }

    /**
     * 查询
     *
     * @param genre
     * @param searchMessage
     * @return
     * @throws Exception
     */
    @Override
    public List<Register> getRegister(String genre, String searchMessage) {

        List<Register> list = null;
        if (genre.equals("id")) {
             list = registerDao.getRegisterId(searchMessage);
        }
        if (genre.equals("phone")) {
             list = registerDao.getRegisterPhone(searchMessage);
        }
        return list;
    }


}
